package com.mmf.service.imp;

import com.mmf.dao.ICommentDao;
import com.mmf.dao.IGoodsDao;
import com.mmf.dto.ResponseModel;
import com.mmf.model.*;
import com.mmf.service.ICommentService;
import com.mmf.utils.Md5;
import com.mmf.utils.UploadImage;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by MMF on 2017-10-18.
 * 商品评论
 */
@Service("commentServiceImpl")
public class CommentServiceImpl implements ICommentService<Comment> {
    @Resource
    private ICommentDao commentDao;

    @Override
    public ResponseModel insert(Comment object) {
        return null;
    }

    @Override
    public ResponseModel insertComment(Comment comment, MultipartHttpServletRequest request) {
        //先插入发布的详情数据，获取到对应的详情的id的值
        int count = commentDao.insert(comment);
        if (count < 1) {
            return new ResponseModel(-1, "添加评论失败");
        }
        int id = comment.getId();
        UploadImage.uploadImage(id, request, new UploadImage.LoadSuccess() {
            @Override
            public void insertDate(String path, int id, int type) {
                commentDao.insertImage(path, id, type);
            }
        });
        return new ResponseModel(0, "","添加评论成功");
    }

    @Override
    public ResponseModel delete(int id) {
        ResponseModel model = new ResponseModel(0, "删除评论成功");
        int count = commentDao.delete(id);
        if (count < 1) {
            return new ResponseModel(-1, "删除评论失败");
        }
        return model;
    }

    @Override
    public ResponseModel update(Comment object) {
        ResponseModel model = new ResponseModel<Comment>(0, "修改成功");
        Comment comment = commentDao.update(object);
        if (comment == null) {
            return new ResponseModel(-1, "修改失败");
        }
        model.setData(comment);
        return model;
    }

    @Override
    public ResponseModel find(int id) {
        ResponseModel model = new ResponseModel<Comment>(0, "查找成功");
        Comment comment = commentDao.find(id);
        if (comment == null) {
            return new ResponseModel(-1, "查找失败");
        }
        List<GoodsImg> imgList = commentDao.getImageList(comment.getId());
        comment.setImgList(imgList);
        model.setData(comment);
        return model;
    }

    @Override
    public ResponseModel list(Page page, String userId, String goodsId) {
        ResponseModel model = new ResponseModel<List<Comment>>(0, "获取列表成功");
        List<Comment> list = commentDao.list(page,userId,goodsId);
        if (list != null && list.size() > 0) {
            for (Comment comment : list) {
                List<GoodsImg> imgList = commentDao.getImageList(comment.getId());
                comment.setImgList(imgList);
            }
        }
        model.setData(list);
        return model;
    }
}
